import { login, logout } from '@/api/login'
import {
  getToken,
  setToken,
  removeToken,
  getUserId,
  setUserId,
  removeUserId,
  setRoleId,
  removeRoleId,
  setUserName,
  removeUserName,
  setName,
  removeName,
  setUserTel,
  removeUserTel,
  setProvinceName,
  removeProvinceName,
  setProvinceCode,
  removeProvinceCode,
  setCityName,
  removeCityName,
  setCityCode,
  removeCityCode,
  setAreaName,
  removeAreaName,
  setAreaCode,
  removeAreaCode,
  setRoleType,
  removeRoleType,
  setOfficeType,
  removeOfficeType,
  setOfficeId,
  removeOfficeId,
  setOfficeProvince,
  removeOfficeProvince,
  setOfficeProvinceCode,
  removeOfficeProvinceCode,
  setOfficeCity,
  removeOfficeCity,
  setOfficeCityCode,
  removeOfficeCityCode,
  setOutsourceType,
  removeOutsourceType,
  setOutsourceProvince,
  removeOutsourceProvince,
  setOutsourceProvinceCode,
  removeOutsourceProvinceCode,
  setOutsourceCity,
  removeOutsourceCity,
  setOutsourceCityCode,
  removeOutsourceCityCode,
  setOutsourceId,
  removeOutsourceId,
  setFactoryId,
  removeFactoryId
} from '@/utils/token'

const user = {
  state: {
    token: getToken(),
    id: getUserId(),
    username: '',
    name: '',
    roleId: null,
    menuList: null,
    location: null,
    roleType: null,
    pointType: null,
    pointId: null,
    tel: null,
    officeType: null,
    preUrl: null
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_ID: (state, id) => {
      state.id = id
    },
    SET_ROLE_ID: (state, roleId) => {
      state.roleId = roleId
    },
    SET_USERNAME: (state, username) => {
      state.username = username
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_MENU_LIST: (state, menuList) => {
      state.menuList = menuList
    },
    SET_LOCATION: (state, location) => {
      state.location = location
    },
    SET_ROLETYPE: (state, roleType) => {
      state.roleType = roleType
    },
    SET_POINTTYPE: (state, pointType) => {
      state.pointType = pointType
    },
    SET_POINTID: (state, pointId) => {
      state.pointId = pointId
    },
    SET_TEL: (state, tel) => {
      state.tel = tel
    },
    SET_OFFICETYPE: (state, officeType) => {
      state.officeType = officeType
    }
  },

  actions: {
    // 通过用户名,密码和验证码登录
    Login ({commit}, loginDto) {
      return new Promise((resolve, reject) => {
        login(loginDto).then(response => {
          const data = response.data
          setToken(data.access_token)
          setUserId(data.id)
          setRoleId(data.roleId)
          setUserName(data.userName)
          setName(data.name)
          setUserTel(data.tel)
          setProvinceName(data.provinceName)
          setProvinceCode(data.provinceCode)
          setCityName(data.cityName)
          setCityCode(data.cityCode)
          setAreaName(data.areaName)
          setAreaCode(data.areaCode)
          setRoleType(data.roleType)
          setOfficeType(data.officeType)
          setOfficeId(data.officeId)
          setOfficeProvince(data.officeProvince)
          setOfficeProvinceCode(data.officeProvinceCode)
          setOfficeCity(data.officeCity)
          setOfficeCityCode(data.officeCityCode)
          setOutsourceType(data.outsourceType)
          setOutsourceProvince(data.outsourceProvince)
          setOutsourceProvinceCode(data.outsourceProvinceCode)
          setOutsourceCity(data.outsourceCity)
          setOutsourceCityCode(data.outsourceCityCode)
          setOutsourceId(data.outsourceId)
          setFactoryId(data.factoryId)
          commit('SET_TOKEN', data.access_token)
          commit('SET_USERNAME', data.userName)
          commit('SET_NAME', data.name)
          commit('SET_ID', data.id)
          commit('SET_ROLE_ID', data.roleId)
          commit('SET_MENU_LIST', data.menu)
          commit('SET_LOCATION', data.location)
          commit('SET_ROLETYPE', data.roleType)
          commit('SET_POINTTYPE', data.pointType)
          commit('SET_POINTID', data.pointId)
          commit('SET_TEL', data.tel)
          commit('SET_OFFICETYPE', data.officeType)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    Logout ({commit}) {
      return new Promise((resolve, reject) => {
        logout().then(() => {
          commit('SET_ID', -1)
          commit('SET_ROLE_ID', [])
          commit('SET_TOKEN', '')
          commit('SET_MENU_LIST', [])
          commit('SET_OFFICETYPE', '')
          removeToken()
          removeUserId()
          removeRoleId()
          removeUserName()
          removeName()
          removeUserTel()
          removeProvinceName()
          removeProvinceCode()
          removeCityName()
          removeCityCode()
          removeAreaName()
          removeAreaCode()
          removeRoleType()
          removeOfficeType()
          removeOfficeId()
          removeOfficeProvince()
          removeOfficeProvinceCode()
          removeOfficeCity()
          removeOfficeCityCode()
          removeOutsourceType()
          removeOutsourceProvince()
          removeOutsourceProvinceCode()
          removeOutsourceCity()
          removeOutsourceCityCode()
          removeOutsourceId()
          removeFactoryId()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    }
  }
}

export default user
